package org.mspring.mlog.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.mspring.mlog.dao.CommentDao;
import org.mspring.mlog.entity.Comment;
import org.mspring.mlog.entity.constants.CacheNames;
import org.mspring.platform.cache.Cache;
import org.mspring.platform.cache.CacheManager;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.hibernate.common.EnhancedRule;
import org.mspring.platform.dao.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class CommentService {

    private CommentDao commentDao;
    private CacheManager cacheManager;
    private Cache commentListCache;

    /**
     * 查找文章对应的评论
     *
     * @param postId
     * @return
     */
    public List<Comment> listByPost(long postId) {
        List<Comment> comments = commentListCache.get(Long.toString(postId));
        if (comments != null) {
            return comments;
        }
        comments = commentDao.listByPostAndStatus(postId, Comment.Status.APPROVED);
        if (comments != null) {
            commentListCache.set(Long.toString(postId), comments);
        }
        return comments;
    }

    /**
     * 查找最近的num条评论
     *
     * @param num
     * @return
     */
    public List<Comment> recent(int num) {
        EnhancedRule rule = new EnhancedRule();
        rule.addOrder(Order.desc("createTime"));
        rule.setOffset(0);
        rule.setPageSize(num);
        rule.add(Restrictions.eq("status", Comment.Status.APPROVED));
        return commentDao.list(rule);
    }

    /**
     * 统计总共有多少评论，只统计已审核通过的
     *
     * @return
     */
    public long countComment() {
        return commentDao.countComment();
    }

    /**
     * 发表评论
     *
     * @param comment
     * @return
     */

    public Comment insert(Comment comment) {
        if (comment.getCreateTime() == null) {
            comment.setCreateTime(new Date());
        }
        Long id = commentDao.insert(comment);
        if (id != null) {
            commentListCache.remove(String.valueOf(comment.getPost().getId()));
        }
        return comment;
    }

    public void delete(Long... id) {
        commentDao.delete(id);
    }

    /**
     * 更新评论状态
     *
     * @param id
     * @param status
     */

    public void updateStatus(long id, String status) {
        commentDao.updateStatus(id, status);
    }

    /**
     * 审核通过
     *
     * @param ids
     */

    public void approved(long... ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        for (long id : ids) {
            commentDao.updateStatus(id, Comment.Status.APPROVED);
        }
    }

    /**
     * 标记为垃圾评论
     *
     * @param ids
     */

    public void spam(long... ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        for (long id : ids) {
            commentDao.updateStatus(id, Comment.Status.SPAM);
        }
    }

    /**
     * 移入回收站
     *
     * @param ids
     */

    public void recycle(long... ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        for (long id : ids) {
            commentDao.updateStatus(id, Comment.Status.RECYCLE);
        }
    }

    public Page<Comment> listPage(QueryBuilder queryBuilder, Page<Comment> page) {
        return commentDao.listPage(queryBuilder, page);
    }

    public Comment get(long id) {
        return commentDao.get(id);
    }

    public Comment getBy(String property, Object value) {
        return commentDao.getBy(property, value);
    }

    @Autowired
    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    @Autowired
    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        this.commentListCache = this.cacheManager.getCache(CacheNames.COMMENT_LIST_CACHE);
    }

}
